<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\CheckUser;
use backend\models\QueryForm;
use backend\models\DataForm;
use backend\models\ReportForm;
use yii\helpers\Json;
use backend\models\OrderForm;
use backend\models\OrderLogic;
use yii\helpers\ArrayHelper;
use common\models\Employees;
use common\models\Process;
use common\models\User;
use common\models\Department;
use common\models\Customer;
use common\models\Product;
use backend\models\TaskForm;
use common\models\CustomerOrder;
use common\models\OrderProduct;
use common\models\TransferUndertake;
use backend\models\ExecuteForm;
use common\models\EmpDepart;

/**
 * Task controller
 */
class TaskController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['preparelist', 'execute', 'getemployee'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * [actionPreparelist 待执行任务列表]
     * @return [type] [mix]
     */
    public function actionPreparelist()
    {
        $employee = User::findOne(Yii::$app->user->id)->employee;

        if ($employee) {
            $data_form = new TaskForm;
            if ($data_form->load(Yii::$app->request->get()) && $data_form->validate()) {
                $data = $data_form->query();
            }

            $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
            $model = $data->offset($pages->offset)->limit($pages->limit)->all();
            /*员工数据*/
            $employee = Employees::find()->all();
            $employee = ArrayHelper::map($employee, 'id', 'realname');

            return $this->render('preparelist', ['model' => $model, 'pages' => $pages, 'employee' => $employee]);
        }
    }

    /**
     * 执行任务
     */
    public function actionExecute($id=null)
    {
        $model = new ExecuteForm;
        $transfer_undertake = TransferUndertake::findOne($id);
        $customer_order = CustomerOrder::find()->where(['order' => $transfer_undertake->oid])->one();
        $process = Process::find()->where(['pcode' => $transfer_undertake->pcode])->all();
        $department = Department::find()->asArray()->all();
        $department = ArrayHelper::map($department, 'id', 'name');
        $order = OrderProduct::find()->where(['oid' => $transfer_undertake->oid])->one();
        $product = Product::find()->asArray()->all();
        $product = ArrayHelper::map($product, 'id', 'name');
        $customer = Customer::find()->asArray()->all();
        $customer = ArrayHelper::map($customer, 'id', 'username');

        return $this->render('execute', ['pcode' => $transfer_undertake->pcode, 'customer' => $customer, 'customer_order' => $customer_order, 'product' => $product, 'model' => $model, 'transfer_undertake' => $transfer_undertake, 'process' => $process, 'department' => $department, 'order' => $order]);
    }

    /**
     * 异步获取承接部门员工
     */
    public function actionGetemployee()
    {
        $pid = trim(Yii::$app->request->get()['pid']);
        $process = Process::findOne($pid);
        if ($process) {
            $emp_dep = EmpDepart::find()->where(['did' => $process->undertake_depart])->all();
            $emp_arr = [];
            foreach ($emp_dep as $key => $val) {
                $emp_arr[$key]['id'] = $val->employee->id;
                $emp_arr[$key]['realname'] = $val->employee->realname;
            }
            if ($emp_arr) {

                return JSON::encode([
                    'flag' => true,
                    'employee' => $emp_arr,
                    'control' => $process->control
                ]);
            }
        }

        return JSON::encode([
                'flag' => false
            ]);
    }


}    